package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.TipsMessag;
import com.cuckoo.cuckoo_app2.ui.home.HomeFragment;
import com.cuckoo.cuckoo_app2.utils.MessageCodeUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class HomeTipsMessagAdapter extends RecyclerView.Adapter<HomeTipsMessagAdapter.ViewHolder> {

    private Context context;
    private List<TipsMessag> dataList;
    private int color_success, color_fail, color_warning, color_grey, color_white, background_image;
    private HomeFragment.ButtonClickListener buttonClickListener;


    public HomeTipsMessagAdapter(Context context, List<TipsMessag> dataList, HomeFragment.ButtonClickListener buttonClickListener) {
        this.context = context;
        this.dataList = dataList;
        this.color_success = ContextCompat.getColor(context, R.color.success);
        this.color_fail = ContextCompat.getColor(context, R.color.fail);
        this.color_warning = ContextCompat.getColor(context, R.color.warning);
        this.color_grey = ContextCompat.getColor(context, R.color.text_grey);
        this.color_white = ContextCompat.getColor(context, R.color.white);

        this.background_image = ContextCompat.getColor(context, R.color.background_image);
        this.buttonClickListener = buttonClickListener;
    }

    @NonNull
    @Override
    public HomeTipsMessagAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_tips_messag_compon, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TipsMessag tips = dataList.get(position);

        holder.tips_title.setText(tips.getTitle());
        holder.tips_messag.setText(tips.getMessag());

//       SUCCESS(200, "成功"),FAIL(500, "失败"), WARNING(300, "警告"),
        MessageCodeUtils codeUtils = MessageCodeUtils.fromCode(tips.getStart());
        switch (codeUtils) {
            case SUCCESS:
                holder.tips_messag.setTextColor(color_white);
                holder.tips_title.setTextColor(color_white);
                holder.tips_date.setTextColor(color_white);
                holder.tips_add.setTextColor(color_white);

                holder.tips_cardView.setCardBackgroundColor(color_success);
                holder.tips_add.setBackgroundColor(color_success);
                break;

            case WARNING:
                holder.tips_messag.setTextColor(color_white);
                holder.tips_title.setTextColor(color_white);
                holder.tips_date.setTextColor(color_white);
                holder.tips_add.setTextColor(color_white);

                holder.tips_cardView.setCardBackgroundColor(color_fail);
                holder.tips_add.setBackgroundColor(color_fail);
                break;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        holder.tips_date.setText(dateFormat.format(tips.getDate()));

        Picasso.get()
                .load(tips.getImageUrl())
                .placeholder(R.drawable.ic_launcher_foreground)
                .into(holder.tips_image);

        holder.tips_linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到网页
                buttonClickListener.onEnter(tips.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<TipsMessag> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView tips_image;
        TextView tips_title;
        TextView tips_date;
        TextView tips_messag;
        LinearLayout tips_linearLayout;
        CardView tips_cardView;
        TextView tips_add;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tips_linearLayout = itemView.findViewById(R.id.tips_linearLayout);
            tips_image = itemView.findViewById(R.id.tips_image);
            tips_title = itemView.findViewById(R.id.tips_title);
            tips_date = itemView.findViewById(R.id.tips_date);
            tips_messag = itemView.findViewById(R.id.tips_messag);
            tips_cardView = itemView.findViewById(R.id.tips_cardView);
            tips_add = itemView.findViewById(R.id.tips_add);
        }
    }
}
